package com.ctjsoft.datacollection.controller;

import cn.hutool.core.math.MathUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.ctjsoft.datacollection.core.povo.Result;
import com.ctjsoft.datacollection.entity.KScript;
import com.ctjsoft.datacollection.service.KScriptService;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

@Api(tags = "首页统计API")
@RestController
@RequestMapping("/dataCollection/monitor")
public class HomeController {

    @Autowired
    KScriptService scriptService;

    /**
     * 首页监控任务统计
     *
     * @return {@link Result}
     */
    @ApiOperation(value = "首页监控任务统计")
    @PostMapping("/taskCount")
    public Result<JSONObject> taskCount() {
        Integer transTaskNum = 0;
        Integer jobTaskNum = 0;
        Integer totalTaskNum = 0;
        List<KScript> list = scriptService.taskCount();
        for (int i = 0; i < list.size(); i++) {
            KScript script = list.get(i);
            if (script.getScriptType() == "1" || script.getScriptType().equals("1")) {
                // 统计运行的转换数量
                transTaskNum = script.getNum();
            } else if (script.getScriptType() == "0" || script.getScriptType().equals("0")) {
                // 统计运行的作业数量
                jobTaskNum = script.getNum();
            }
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("totalTaskNum", transTaskNum + jobTaskNum);
        jsonObject.put("transTaskNum", transTaskNum);
        jsonObject.put("jobTaskNum", jobTaskNum);
        return Result.ok(jsonObject);
    }

    /**
     * 首页监控任务统计
     *
     * @return {@link Result}
     */
    @ApiOperation(value = "首页监控任务统计")
    @PostMapping("/runStatus")
    public Result<List<JSONObject>> runStatus() {
        List<JSONObject> list = new ArrayList<>();
        for(int i=0;i<7;i++){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("runTime","2019-11-"+(20+i));
            jsonObject.put("jobRunNum",1+ RandomUtil.randomInt(1,10));
            jsonObject.put("TransRunNum",2);
            list.add(jsonObject);
        }

//        list.add(new HomeMonitorTaskRunRes().setRunTime("2019-11-20").setJobRunNum(5).setTransRunNum(2));
//        list.add(new HomeMonitorTaskRunRes().setRunTime("2019-11-21").setJobRunNum(1).setTransRunNum(12));
//        list.add(new HomeMonitorTaskRunRes().setRunTime("2019-11-22").setJobRunNum(3).setTransRunNum(5));
//        list.add(new HomeMonitorTaskRunRes().setRunTime("2019-11-23").setJobRunNum(8).setTransRunNum(6));
//        list.add(new HomeMonitorTaskRunRes().setRunTime("2019-11-24").setJobRunNum(6).setTransRunNum(8));
//        list.add(new HomeMonitorTaskRunRes().setRunTime("2019-11-25").setJobRunNum(2).setTransRunNum(12));
//        list.add(new HomeMonitorTaskRunRes().setRunTime("2019-11-26").setJobRunNum(4).setTransRunNum(20));
        return Result.ok(list);
    }
}
